package com.nfha.algorithm.taskassign;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;



public class Employee {

	


	public String id, portCode;

	public Set<String> skillIds = new HashSet<>();

	
	public List<TaskInfo> doneTasks=new ArrayList<>()
			,toDoTasks=new ArrayList<>()
			,doingTasks=new ArrayList<>()
			,tempTasks=new ArrayList<>()
			;

	public Employee(String id, String... skillIds) {
		super();
		this.id = id;
		for (String skillId : skillIds) {
			this.skillIds.add(skillId);
		}
	}
	
	int totalTaskCount() {
		return doneTasks.size()+toDoTasks.size()+doingTasks.size();
	}

	// 是否满足技能
	int hasSkill(String... skillIds) {
		int re = 0;
		for (String skillId : skillIds) {
			if (this.skillIds.contains(skillId)) {
				re++;
			}
		}
		return re;
	}
	
	
	
	public boolean canDoTask(TaskInfo taskInfo) {
		boolean re=false;
		String remark="办理正常";
		
		
		if(this.tempTasks.size()<=2) {
			re=true;
		}else  {
			Set<String> donePortTop3Set=new HashSet<>();
	    	for(int i=0;i<3;i++) {
	    		donePortTop3Set.add(tempTasks.get(i).portCode);
	    	}
	    	
			
			if(donePortTop3Set.size()==1 ) {
				//如果前三个码头相同
				String samePort=donePortTop3Set.iterator().next();
				if(tempTasks.size()>5) {
					re=true;
					remark="我在同一个码头 "+samePort+" 已经达到5次，不能再去任何码头";
				}else if(taskInfo.portCode.equals(samePort)) {
				    //这时只能再办一次同一码头的
				    re=true;
				}else {
					re=false;
					remark="我在同一个码头 "+samePort+" 已经超过3次，不能再去其他码头："+taskInfo.portCode;
				}
				
			}
			//else if(PortCodeEnum.PORT_CODE_0.value.equals(taskInfo.portCode)) {
			//	re=true;
			//	remark="我在跨码头 已经超过2次，：但我可以去任意码头:"+taskInfo.portCode;
			//}
			else {
				remark="我在跨码头 已经达到3次，不能再接码头："+taskInfo.portCode;
			}
			
	    }
		
		
		
	    taskInfo.remark="人员:"+id+"  "+remark;
		
		
		return re;
	}

	protected static int hasSkill(Employee[] employees, String... skillIds) {
		int re = 0;
		for (String skillId : skillIds) {
			for (Employee employee : employees) {
				if (employee.skillIds.contains(skillId)) {
					re++;
					break;
				}
			}
		}
		return re;
	}
	
	

	// 按人员id排序生成组id
	protected static String genGroupId(Employee... employees) {
		String re = "";
		String[] str = new String[employees.length];

		for (int i = 0; i < str.length; i++) {
			str[i] = employees[i].toString();
		}
		Arrays.sort(str);
		for (int i = 0; i < str.length; i++) {
			re += str[i] + "|";
		}
		return re;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.id;
	}

	protected static Employee[] appendTo(Employee[] employees, Employee... ems) {
		Set<Employee> re = new HashSet<>();
		for (Employee employee : employees) {
			re.add(employee);
		}
		for (Employee employee : ems) {
			re.add(employee);
		}
		return re.toArray(new Employee[re.size()]);
	}

	protected static int isContain(Employee[] employees, Employee[] subEmployees) {
		int re = 0;

		for (Employee subEmployee : subEmployees) {
			try {
				for (Employee employee : employees) {

					if (subEmployee == employee) {
						re++;
						break;
					}

				}
			} catch (Exception ex) {
				 //ex.printStackTrace();
			}
		}

		return re;
	}

	
    public static void print(Collection<Employee> employees) {
    	System.out.println("用户ID 	 待办                  在办  	  已办  	合计   最近去过的码头  总共 去过的码头");
		for (Employee emp : employees) {
			String ports="",tempports="";
			for(TaskInfo ti:emp.doneTasks) {
				ports+=ti.portCode+",";
			}
			for(TaskInfo ti:emp.tempTasks) {
				tempports+=ti.portCode+",";
			}
			String msg = MessageFormat.format(
					"id:{0}  	 {1}  	 {2}  	 {3}  	 {4}    {5}     {6}", emp.id,
					emp.toDoTasks.size(), emp.doingTasks.size(),
					emp.doneTasks.size(), 
					
					emp.totalTaskCount()
					,tempports
					,ports
					);
			System.out.println(msg);
		}
    }
	
	

}
